//
//  STHeartViewController.m
//  BasicAnimation-OC
//
//  Created by Stan on 2017-02-13.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STHeartViewController.h"

@interface STHeartViewController ()

@end

@implementation STHeartViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *heartImage = [UIImage imageNamed:@"heart"];
    
    //        设置❤️图层的位置
    CALayer *heartLayer = [self createLayerWithPosition:CGPointMake(self.view.center.x, 100) backgroundColor:[UIColor whiteColor]];

    
    //        将❤️图层绘制上图片
    heartLayer.contents = (__bridge id _Nullable)([heartImage CGImage]);

    
    //        为❤️图层添加动画
    [heartLayer addAnimation:[self createAnimation] forKey:nil];

    
    
    
    //        添加照片层
    UIImageView *photoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"photo"]];

    [self.view addSubview:photoImageView];
    
    //        设置照片的位置
    photoImageView.center = self.view.center;
    photoImageView.bounds = CGRectMake(0, 0, 300, 300);

    
    
    //        设置照片消失的动画
    [UIView animateWithDuration:2.0 animations:^{
        photoImageView.alpha = 0;
    }];

 
}

//抽取创建Layer的公共方法
- (CALayer *)createLayerWithPosition:(CGPoint)position backgroundColor:(UIColor *)backgroundColor{
    //创建calayer
    CALayer *layer = [CALayer layer];
    //设置位置和大小
    
    layer.position = position;
    layer.bounds = CGRectMake(0, 0, 100, 100);

    
    //设置背景颜色
    layer.backgroundColor = [backgroundColor CGColor];

    //把layer添加到UIView的layer上
    [self.view.layer addSublayer:layer];

    
    return layer;

}

//创建基础Animation
- (CABasicAnimation *)createAnimation{
    //创建动画对象
    CABasicAnimation *scaleAni = [CABasicAnimation animation];
    //设置动画属性
    scaleAni.keyPath = @"transform.scale";
    
    //设置动画的起始位置。也就是动画从哪里到哪里。不指定起点，默认就从positoin开始
    
    scaleAni.toValue = @(0.5);
    
    //动画持续时间
    scaleAni.duration = 1.0;
    
    //        动画的节奏
    scaleAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    

    
    //        动画持续重复次数
    scaleAni.repeatCount = CGFLOAT_MAX;

    
    return scaleAni;
}

@end
